package portal.controller;

import beans.HttpConfig;
import beans.TokenValue;
import net.HttpRequest;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(name = "SearchQueueListServlet")
public class SearchQueueListServlet extends HttpServlet {
    private static final long serialVersionUID = -3814860962562928460L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if(!HttpConfig.fake){     //真数据

            String token = TokenValue.TOKEN.getTokenValue(HttpConfig.userName,HttpConfig.passWord);
            if(token!=null) {
                String json = HttpRequest.sendGet(HttpConfig.queueListUrl, "token="+token);
//                System.out.println("接收到的："+json);
                response.getWriter().print(json);
            }

        }else{   //假数据
            JSONObject result = new JSONObject();

            ArrayList<JSONObject> queueslist = new ArrayList<>();
            for(int i =0;i<23;i++){
                JSONObject content1 = new JSONObject();
                content1.put("QUEUE_NAME","test"+i);
                content1.put("PRIO","40");
                content1.put("STATUS","Open:Active");
                content1.put("MAX","-");
                content1.put("JL/U","-");
                content1.put("JL/P","-");
                content1.put("JL/H","-");
                content1.put("NJOBS","0");
                content1.put("PEND","0");
                content1.put("RUN","0");
                content1.put("SUSP","0");
                queueslist.add(content1);
            }
            JSONArray data = JSONArray.fromObject(queueslist);

            result.put("result","success");
            result.put("message","获取队列成功");
            result.put("data",data);

//            System.out.println("自己封装的假的json："+result.toString());
            response.getWriter().print(result.toString());

        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }


}
